package com.wfcm.wxUitls;

import com.wfcm.entity.WfNewMessageEntity;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestMessage {
    public static void main(String[] args) {
        WfNewMessageEntity entity = new WfNewMessageEntity();
        entity.setUserId(90881);
        entity.setForeignId(888);
        entity.setType(2);
        entity.setStatus(0);
        entity.setTitle("好芳法课堂双十一全场满减，抽苹果手机！");
        entity.setContent("【好芳法课堂】双十一好芳法课堂抽奖结果公布！\n" +
                "儿童电话手表中奖码：WA-40925-DZDB（手机号183****7716）；\n" +
                "苹果iPad中奖码：IP-87226-BIVL（手机号134****8800）；\n" +
                "苹果iPhoneXR中奖码：XR-129948-BZAF（手机号158****7973）。\n" +
                "恭喜以上获奖者！请于2018年11月20日前与好芳法课堂课程顾问联系。");
        entity.setRemark("");
        entity.setUrl("https://www.baidu.com");
        entity.setRichText("");
        getAll();
    }

    private static Connection getConn() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://pc-2zeth884p64r5rbamo.mysql.polardb.rds.aliyuncs.com/wf_course_v3?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
        String username = "wf_course_v3";
        String password = "QzPIlslh4ql4KRS02qZu2paXaUqDHm";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    private static int insert(WfNewMessageEntity entity) {
        Connection conn = getConn();
        int i = 0;
        String sql = "insert into wf_new_message (userId,foreignId,type,status,title,content,remark,url,richText)" +
                " values(?,?,?,?,?,?,?,?,?)";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setInt(1, entity.getUserId());
            pstmt.setInt(2, entity.getForeignId());
            pstmt.setInt(3, entity.getType());
            pstmt.setInt(4, entity.getStatus());
            pstmt.setString(5, entity.getTitle());
            pstmt.setString(6, entity.getContent());
            pstmt.setString(7, entity.getRemark());
            pstmt.setString(8, entity.getUrl());
            pstmt.setString(9, entity.getRichText());
            i = pstmt.executeUpdate();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    private static Integer getAll() {
        Connection conn = getConn();
        String sql = "select userId from wf_member where telephone is not null order by userId";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            ExecutorService service = Executors.newFixedThreadPool(10);
            List<Integer> list = new ArrayList<>();
            while (rs.next()) {
                list.add(rs.getInt(1));
            }
            System.err.println(list.size());
            list.forEach(id -> {
                service.execute(() -> {
                    try {
                        WfNewMessageEntity entity = new WfNewMessageEntity();
                        entity.setUserId(id);
                        entity.setForeignId(888);
                        entity.setType(2);
                        entity.setStatus(0);
                        entity.setTitle("双十一全场满减还有抽苹果手机！");
                        entity.setContent("【好芳法课堂】双十一好芳法课堂抽奖结果公布！\n" +
                                "儿童电话手表中奖码：WA-40925-DZDB（手机号183****7716）；\n" +
                                "苹果iPad中奖码：IP-87226-BIVL（手机号134****8800）；\n" +
                                "苹果iPhoneXR中奖码：XR-129948-BZAF（手机号158****7973）。\n" +
                                "恭喜以上获奖者！请于2018年11月20日前与好芳法课堂课程顾问联系。");
                        entity.setRemark("");
                        entity.setUrl("https://1111.wfkids.net");
                        entity.setRichText("");
                        insert(entity);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
